<template>
  <transition name="slide">
    <music-list :songs="songs" :title="title" :bg-image="bgImage"></music-list>
  </transition>
</template>

<script type="text/ecmascript-6">
  import {mapGetters} from 'vuex'
  import {singer} from "../../store/getters";
  import {getSingerDetail} from "api/singer";
  import {ERR_OK} from "api/config";
  import {createSong,getSongVkey} from "../../common/js/song";
  import musicList from 'components/music-list/music-list'


  export default {
    name: "singer-detail",
    data() {
      return{
        songs:[]
      }
    },
    computed: {
      title(){
        return this.singer.name
      },
      bgImage(){
        return this.singer.avatar
      },
      ...mapGetters([
        'singer' // 对应的是store的getters
      ])


    },
    created() {
   // console.log(createSong)
      this._getDetail()
    },
    components:{
      musicList
    },
    methods: {

      _getDetail() {
      //  console.log(this.singer);
        if (!this.singer.id) {
          this.$router.push('/singer')
          return
        }
        getSingerDetail(this.singer.id).then((res) => {
          if (res.code === ERR_OK) {
           //console.log(res.data.list);
            this.songs =this._formatSongs(res.data.list)
            console.log(this.songs);
          }
        })


      },
      //重组 res.data.list 数据,只拿需要的
      _formatSongs(list){
        let result = []
        list.forEach((item)=>{
          // 解构赋值-拿到item 下的 musicData 列表数据
          let {musicData} = item
          //------------- 更新的加上vkey
          getSongVkey(musicData.songmid).then((res) => {
            const vkey = res.data.items[0].vkey;
            if (musicData.songid && musicData.albummid) {
              result.push(createSong(musicData, vkey))
            }
          })

        })
        return result
      },
      _normalizeSongs(list){
        let ret = []
        list.forEach((item)=>{
          let {musicData} = item // 解构赋值
          if (musicData.songid&& musicData.albummid){
            ret.push(createSong(musicData))

          }
        })
        return ret
      }
    }

  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">

  @import "~common/stylus/variable"

  .slide-enter-active, .slide-leave-active
    transition: all 0.3s

  .slide-enter, .slide-leave-to
    transform translate3d(100%, 0, 0)
</style>
